package com.itboye.jinpuyiyuan.activity.message;

import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2018/1/15 0015.
 */

public class AddRemarkActivity extends BaseActivity implements Observer {

    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;

    private BranchAddressPresenter presenter;
    public static int ADDSUCCES=25;
    private EditText fankui_et;
    private TextView real_ok;
    private ArrayList<String> list;
    private int illUid;//患者uid
    private String uid;//医生本身id
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_remark);
        leftTv.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        tvTitle.setText("添加备注");
        presenter= new BranchAddressPresenter(this);
        uid= (String) SPUtils.get(getApplication(),null, Const.UID,"");
        illUid=getIntent().getIntExtra("illId",0);
        list = getIntent().getStringArrayListExtra("list");

    }
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){

            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;

            case R.id.real_ok:
                if (list!=null) {
                    if (list.size() > 0) {
                        if (isSame(fankui_et.getText().toString())) {
                            MAlert.alert("该患者已经拥有了该标签");
                        } else {
                            presenter.addRemark(illUid + "", fankui_et.getText().toString(), uid);
                        }

                    }
                }
                presenter.addRemark(illUid + "", fankui_et.getText().toString(), uid);
                break;

        }
    }

    public boolean isSame(String tagName){
        for (int i = 0; i < list.size(); i++) {
            if (fankui_et.getText().toString().equals(list.get(i))) {

                return true;
            }

        }
        return false;
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity resultEntity = handlerError(arg);
        if (resultEntity!=null){

            if (resultEntity.getEventType()== BranchAddressPresenter.By_UserTags_add_success){
//                MAlert.alert("用户标签添加成功");
                setResult(AddRemarkActivity.ADDSUCCES);
                AddRemarkActivity.this.finish();
            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_UserTags_add_fail){

            }
        }
    }
}
